package br.com.admin.datatable;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.admin.domain.DataSource;
import br.com.admin.service.DataSourceService;
import br.com.commons.utility.datatable.handler.DataTableHandler;

@Component
public class DataSourceHandler extends DataTableHandler {
	
	@Autowired
	private DataSourceService dataSourceService;
	
	@Override
	protected String[] getColumns() {
		String[] columns = {"id", "driver", "url", "username"};
		return columns;
	}
	
	@Override
	protected List<?> getListObjects(List<String> searchColumns, String searchValue, String orderColumn, String orderDirection, int firstPosition, int numberOfRecords) {
		return dataSourceService.listForDataTable(searchColumns, searchValue, orderColumn, orderDirection, firstPosition, numberOfRecords);
	}

	@Override
	protected Long getTotalNumberOfRegister(List<String> searchColumns, String searchValue) {
		return dataSourceService.countForDataTable(searchColumns, searchValue);
	}

	@Override
	protected String[] getObject(Object object) {
		DataSource dataSource = DataSource.class.cast(object);
		String[] dataSourceStr = new String[dataTable.getNumberColumns()];
		dataSourceStr[0] = String.valueOf(dataSource.getId());
		dataSourceStr[1] = dataSource.getDriver();
		dataSourceStr[2] = dataSource.getUrl();
		dataSourceStr[3] = dataSource.getUsername();
		return dataSourceStr;
	}

}
